<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Shortstatistic extends Model
{
    protected $table = 'short_statistic';
    protected $primaryKey = 'id';

    public static function shortYesterday($shortIds)
    {
        return self::whereIn("short_id",$shortIds)->get();
    }

    public static function shortMonth($shortId,$phase)
    {
        return DB::select('select sum(uv) as uv,short_id from short_statistic where phase >= '.$phase.' and short_id in '.$shortId.' group by short_id');
    }

    public static function shortDetialMonth($short_id,$phase)
    {
        return self::where("short_id",$short_id)->where("phase",">=",$phase)->sum("uv");
    }

    public static function shortSumMonth($short_id)
    {
        return self::where("short_id",$short_id)->sum("uv");
    }

    public static function shortStatistic($short_id,$start,$end)
    {
        return self::where("short_id",$short_id)->whereBetween("phase",[$start,$end])->get();
    }

    public static function shortStatisticPhase($short_id,$phase)
    {
        return self::select("uv")->where("short_id",$short_id)->where("phase",$phase)->first();
    }

    public static function scanScoreCreate($openid,$score)
    {
        return self::insert(["openid"=>$openid,"score"=>$score,"created_at"=>time()]);
    }
}